<template>
  <!-- 表单 -->
  <el-form ref="empForm" :model="empForm" :rules="empRules" label-width="120px" label-position="left" class="demo-ruleForm">
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="empForm.username" style="width:95%" placeholder="请输入姓名" />
    </el-form-item>
    <el-form-item label="手机:" prop="mobile">
      <el-input v-model="empForm.mobile" style="width:50%" placeholder="请输入手机号" />
    </el-form-item>
    <el-form-item prop="timeOfEntry" label="入职时间:">
      <el-date-picker v-model="empForm.timeOfEntry" style="width:50%" placeholder="请选择入职时间:" />
    </el-form-item>
    <el-form-item prop="formOfEmployment" label="聘用形式:">
      <el-select v-model="empForm.formOfEmployment" style="width:50%" placeholder="请选择">
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item prop="workNumber" label="工号:">
      <el-input v-model="empForm.workNumber" style="width:95%" placeholder="请输入工号" />
    </el-form-item>
    <el-form-item prop="departmentName" label="部门:">
      <el-input ref="deptName" v-model="empForm.departmentName" style="width:75%" placeholder="请选择部门" @focus="showDept=true" />
      <el-tree v-if="showDept" style="border:1px solid #f5f7fa; width:75%; margin-top:5px; border-radius:5px;" :data="data" :props="defaultProps" default-expand-all @node-click="handleNodeClick" />
    </el-form-item>
    <el-form-item prop="correctionTime" label="转正时间:">
      <el-date-picker v-model="empForm.correctionTime" style="width:75%" placeholder="请选择转正时间:" />
    </el-form-item>
    <el-form-item>
      <el-button @click="cancelHandler">取消</el-button>
      <el-button type="primary" @click="subHandler">确定</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import EmployeeEnum from '@/constant/employees'
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/emloyees'
import arrayToTree from 'array-to-tree'
export default {
  name: 'Empdialog',

  data() {
    return {
      empForm: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      empRules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          { min: 1, max: 4, message: '用户姓名为1-4位', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'change' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: 'blur' }
        ]
      },
      options: EmployeeEnum.hireType,
      value: '',
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      showDept: false
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    handleNodeClick(data, node) {
      // console.log(data, node)
      if (data.children) {
        return
      } else {
        this.empForm.departmentName = data.name
        this.showDept = false
      }
    },
    // 获取企业的部门列表
    async getDepartments() {
      const res = await getDepartments()
      // console.log(res)
      if (res.code === 10000) {
        // console.log(res.data.depts)
        this.data = arrayToTree(res.data.depts, {
          parentProperty: 'pid',
          customID: 'id'
        })
      }
    },
    // 新增员工
    subHandler() {
      this.$refs.empForm.validate(async result => {
        if (result) {
          // console.log(this.empForm)
          const res = await addEmployee(this.empForm)
          // console.log(res)
          if (res.code === 10000) {
            this.$emit('success')
            this.$message.success('添加员工成功!')
            close()
          }
        }
      })
    },
    // 取消
    cancelHandler() {
      this.$emit('success')
      this.$refs.empForm.resetFields()
    },
    // 清除所有数据
    close() {
      this.$refs.empForm.resetFields()
      this.empForm = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    }
  }
}
</script>

<style lang="scss" scoped>

.demo-ruleForm{

::v-deep.el-tree-node__content{
      height: 40px ;
    }

}
</style>
